﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceModel.Syndication;
using System.Threading;
using TciNewsReader_2014_04.Infrastructure;
namespace TciNewsReader_2014_04.Model
{
    /// <summary>
    ///     Provide a simple, but extensible, wrapper for the SyndicationItem FCL class
    /// </summary>
    public sealed class ItemWrapper : INotifyPropertyChanged
    {
        public string FeedPublisherName { get; private set; }
        public string Md5 { get; private set; }
        private readonly SyndicationItem _item; //(System.ServiceModel)
        private FilterStatus _filterStatus;
        private ItemStatus _itemStatus;
        private FeedInfo _ownerInfo;
        public ItemWrapper(SyndicationItem item, FeedInfo feedInfo, string feedPublisherName)
        {
            _item = item;
            OwnerInfo = feedInfo;
            FeedPublisherName = feedPublisherName;
            ItemStatus = ItemStatus.NotRead;
            FilterStatus = FilterStatus.FilteredIn;
            Md5 = Md5Support.CalculateMd5Hash(GetMd5Key());
        }
        public string ItemTitle
        {
            get { return _item == null ? String.Empty : _item.Title.Text; }
        }
        public string ItemSummary
        {
            get
            {
                if (_item == null) return "No summary";
                if (_item.Summary != null)
                {
                    if (_item.Summary.Text.Length > 0)
                    {
                        return _item.Summary.Text;
                    }
                }
                if (_item.Content != null)
                {
                    var tsc = _item.Content as TextSyndicationContent;
                    if (tsc == null) return "None";
                    return tsc.Text.Length == 0 ? ItemTitle : tsc.Text;
                }
                return "None";
            }
        }
        public DateTime ItemPublishDate
        {
            get { return _item == null ? DateTime.MinValue : _item.PublishDate.DateTime; }
        }
        public string Link
        {
            get
            {
                if (_item != null)
                {
                    if (_item.Links.Count > 0)
                    {
                        return _item.Links[0].Uri.AbsoluteUri;
                    }
                }
                return null;
            }
        }
        public FilterStatus FilterStatus
        {
            [DebuggerStepThrough] get { return _filterStatus; }
            [DebuggerStepThrough]
            set
            {
                if (value != _filterStatus)
                {
                    _filterStatus = value;
                    OnPropertyChanged("FilterStatus");
                }
            }
        }
        public ItemStatus ItemStatus
        {
            [DebuggerStepThrough] get { return _itemStatus; }
            [DebuggerStepThrough]
            set
            {
                if (value != _itemStatus)
                {
                    _itemStatus = value;
                    OnPropertyChanged("ItemStatus");
                }
            }
        }
        public FeedInfo OwnerInfo
        {
            [DebuggerStepThrough] get { return _ownerInfo; }
            [DebuggerStepThrough]
            set
            {
                if (value != _ownerInfo)
                {
                    _ownerInfo = value;
                    OnPropertyChanged("OwnerInfo");
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = Interlocked.CompareExchange(ref PropertyChanged, null, null);
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        private string GetMd5Key()
        {   // melt the item into something reasonably well behaved 
            // that can be represented in the en-US character set
            string result = FeedPublisherName + ItemTitle;
            return result;
        }
        public override string ToString()
        {
            return String.Format("{0} | {1} | {2}",
                ItemPublishDate.ToString("s"),
                FeedPublisherName,
                ItemTitle);
        }
    }
}
